package com.ckzp.jfinal.system.controller;

import com.ckzp.core.Results;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.system.cache.SysConfigCache;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.interceptor.Roles;
import com.ckzp.jfinal.system.model.SysCode;
import com.ckzp.jfinal.system.model.SysCodeColumn;
import com.ckzp.jfinal.system.model.SysDept;
import com.ckzp.jfinal.system.util.SysCodeUtil;
import com.ckzp.plugins.layui.LayuiData;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by 51594 on 2021/4/9.
 */
public class SysCodeController extends BaseController {

    @Admins(roles = "administrator", name = "", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 代码列表页面
     */
    @Admins(roles = "administrator", name = "代码列表页面", log = false)
    public void SysCodeList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("SysCodeList.html");
    }

    /**
     * 列表数据提供API
     */
    @Admins(roles = "administrator", name = "代码列表页面", log = false)
    public void SysCodeListDataApi() {

        PageSearch ps = PageSearch.n().init(getRequest(), "sys_code")
                .orderby(getRequest(), "c_id desc");
        Page<SysCode> pages = ps.paginate(SysCode.dao);
        List<SysCode> datalist = pages.getList();
        for (SysCode st : datalist) {
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }


    /**
     * 列表
     */
    @Admins(roles = "administrator", name = "代码列表页面", log = false)
    public void SysCodeColumnList() {
        int c_id = getParaInt("c_id", 0);
        FormData fd = FormData.dao();
        setAttr("form", fd.setKey(String.valueOf(c_id)));
        render("SysCodeColumnList.html");
    }

    /**
     * 列表
     */
    @Admins(roles = "administrator", name = "代码列表页面", log = false)
    public void SysCodeFormList() {
        int c_id = getParaInt("c_id", 0);
        FormData fd = FormData.dao();
        setAttr("form", fd.setKey(String.valueOf(c_id)));
        render("SysCodeFormList.html");
    }

    /**
     * 列表数据提供API
     */
    @Admins(roles = "administrator", name = "代码列表页面", log = false)
    public void SysCodeColumnListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_code_column")
                .search("c_id", getParaInt("c_id", 0))
                .orderby(getRequest(), "sindex");
        Page<SysCodeColumn> pages = ps.paginate(SysCodeColumn.dao);
        List<SysCodeColumn> datalist = pages.getList();
        for (SysCodeColumn st : datalist) {
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 查看生成的代码
     */
    @Admins(roles = "administrator", name = "查看生成的代码", log = false)
    public void ViewSysCode() {
        int c_id = getParaInt("c_id", 0);
        FormData fd = FormData.dao();
        SysCodeUtil util = SysCodeUtil.n();
        SysCode code = SysCode.dao.findById(c_id);
        fd.addData("config", util.getCodeConfig(code));
        fd.addData("route", util.getCodeRoute(code));
        fd.addData("model", util.getCodeModel(code));
        fd.addData("controller", util.getCodeController(code));
        fd.addData("list", util.getCodeList(code));
        fd.addData("modify", util.getCodeModify(code));
        fd.addData("validate", util.getCodeValidate(code));
        setAttr("form", fd.setKey(code.mod.getStr("model_name")));
        render("ViewSysCode.html");
    }

    /**
     * 添加代码页面
     */
    @Admins(roles = "administrator", name = "添加代码页面", log = false)
    public void AddSysCode() {
        FormData fd = FormData.dao().setActions("ADD");
        SysCode code = new SysCode();
        code.put("package_name", SysConfigCache.getConfigValue("codebase"));
        setAttr("code", code);
        setAttr("form", fd);
        render("ModifySysCode.html");
    }

    /**
     * 修改代码页面
     */
    @Admins(roles = "administrator", name = "修改代码页面", log = false)
    public void EditSysCode() {
        String c_id = getPara("c_id", "");
        SysCode o = SysCode.dao.findById(c_id);
        FormData fd = FormData.dao().setActions("EDIT");
        setAttr("form", fd);
        setAttr("code", o);
        render("ModifySysCode.html");
    }

    /**
     * 修改API
     */
    @Admins(roles = "administrator", name = "修改API", log = false)
    public void ModifySysCodeApi() {
        //取得数据
        Datas data = getDatas();
        String c_id = data.getStr("c_id");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysCode.dao.findById(c_id)
                    .set("table_name", data.getStr("table_name"))
                    .set("table_title", data.getStr("table_title"))
                    .set("table_role", data.getStr("table_role"))
                    .set("package_name", data.getStr("package_name"))
                    .set("model_name", data.getStr("model_name"))
                    .set("model_sname", data.getStr("model_sname"))
                    .set("model_key", data.getStr("model_key"))
                    .set("list_tree", data.getInt("list_tree", 0))
                    .set("list_tool", data.getInt("list_tool", 0))
                    .set("list_searchmore", data.getInt("list_searchmore", 0))
                    .set("form_v_js", data.getInt("form_v_js", 0))
                    .set("form_v_api", data.getInt("form_v_api", 0))
                    .set("form_v_bdel", data.getInt("form_v_bdel", 0))
                    .set("tree_key", data.getStr("tree_key", ""))
                    .set("model_path", data.getStr("model_path"))
                    .set("c_desc", data.getStr("c_desc"))
                    .update();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new SysCode()
                    .set("table_name", data.getStr("table_name"))
                    .set("table_title", data.getStr("table_title"))
                    .set("table_role", data.getStr("table_role"))
                    .set("package_name", data.getStr("package_name"))
                    .set("model_name", data.getStr("model_name"))
                    .set("model_sname", data.getStr("model_sname"))
                    .set("model_key", data.getStr("model_key"))
                    .set("list_tool", data.getInt("list_tool", 0))
                    .set("list_tree", data.getInt("list_tree", 0))
                    .set("tree_key", data.getStr("tree_key", ""))
                    .set("form_v_js", data.getInt("form_v_js", 0))
                    .set("form_v_api", data.getInt("form_v_api", 0))
                    .set("form_v_bdel", data.getInt("form_v_bdel", 0))
                    .set("list_searchmore", data.getInt("list_searchmore", 0))
                    .set("model_path", data.getStr("model_path"))
                    .set("c_desc", data.getStr("c_desc"))
                    .save();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改列值API API
     */
    @Admins(roles = "administrator", name = "修改列值API", log = false)
    public void ModifySysCodeColumnFieldApi() {
        int cc_id = getParaToInt("key_id", 0);
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysCodeColumn.dao.findById(cc_id).set(field, value).update();
        renderJson(JsonResult.n(0, "修改成功"));
    }

    /**
     * 删除接口API
     */
    @Admins(roles = "administrator", name = "删除代码API", log = false)
    public void DelSysCodeApi() {
        int c_id = getParaToInt("c_id", 0);
        int counts = Db.queryInt("select count(*) from sys_code_column where c_id=?", c_id);
        if (counts == 0) {
            if (SysCode.dao.findById(c_id).delete()) {
                renderJson(JsonResult.n(0, "删除成功"));
            } else {
                renderJson(JsonResult.n(9, "删除失败"));
            }
        } else {
            renderJson(JsonResult.n(9, "该记录下有" + counts + "子记录,不能删除"));
        }
    }

    /**
     * 删除接口API
     */
    @Admins(roles = "administrator", name = "删除代码列API", log = false)
    public void DelSysCodeColumnApi() {
        renderJson(DelMoreModel(SysCodeColumn.dao, getParaStr("keys", ""), "删除字段"));
    }

    /**
     * 修改代码列API
     */
    @Admins(roles = "administrator", name = "修改代码列API", log = false)
    public void ChangeSysCodeColumnApi() {
        int cc_id = getParaInt("cc_id", 0);
        String field = getParaStr("field", "");
        String msg = getParaStr("msg", "");
        if (field.indexOf("f_v") > -1) {
            SysCodeUtil.n().initColumnVmsg(cc_id, field);
        }
        renderJson(DoBasePub(SysCodeColumn.dao, cc_id, field, msg));
    }

    /**
     * 初始化表单
     */
    @Admins(roles = "administrator", name = "初始化表单", log = false)
    public void InitSysCodeColumn() {
        int c_id = getParaToInt("c_id", 0);
        renderJson(JsonResult.n(SysCodeUtil.n().InitSysCodeColumn(c_id)));
    }
}